home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / gas / config / obj-bout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-03  |  10.1 KB  |  331 lines

  1. /* b.out object file format
  2.    Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as
  8.    published by the Free Software Foundation; either version 2,
  9.    or (at your option) any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful, but
  12.    WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
  14.    the GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public
  17.    License along with GAS; see the file COPYING.  If not, write
  18.    to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  19.  
  20. #include "as.h"
  21. #include "obstack.h"
  22. const short            /* in: segT   out: N_TYPE bits */
  23.   seg_N_TYPE[] =
  24. {
  25.   N_ABS,
  26.   N_TEXT,
  27.   N_DATA,
  28.   N_BSS,
  29.   N_UNDF,            /* unknown */
  30.   N_UNDF,            /* error */
  31.   N_UNDF,            /* expression */
  32.   N_UNDF,            /* debug */
  33.   N_UNDF,            /* ntv */
  34.   N_UNDF,            /* ptv */
  35.   N_REGISTER,            /* register */
  36. };
  37.  
  38. const segT N_TYPE_seg[N_TYPE + 2] =
  39. {                /* N_TYPE == 0x1E = 32-2 */
  40.   SEG_UNKNOWN,            /* N_UNDF == 0 */
  41.   SEG_GOOF,
  42.   SEG_ABSOLUTE,            /* N_ABS == 2 */
  43.   SEG_GOOF,
  44.   SEG_TEXT,            /* N_TEXT == 4 */
  45.   SEG_GOOF,
  46.   SEG_DATA,            /* N_DATA == 6 */
  47.   SEG_GOOF,
  48.   SEG_BSS,            /* N_BSS == 8 */
  49.   SEG_GOOF,
  50.   SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
  51.   SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
  52.   SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
  53.   SEG_REGISTER,            /* dummy N_REGISTER for regs = 30 */
  54.   SEG_GOOF,
  55. };
  56.  
  57. static void obj_bout_line PARAMS ((int));
  58.  
  59. const pseudo_typeS obj_pseudo_table[] =
  60. {
  61.   {"line", obj_bout_line, 0},    /* source code line number */
  62.  
  63. /* coff debugging directives.  Currently ignored silently */
  64.   {"def", s_ignore, 0},
  65.   {"dim", s_ignore, 0},
  66.   {"endef", s_ignore, 0},
  67.   {"ln", s_ignore, 0},
  68.   {"scl", s_ignore, 0},
  69.   {"size", s_ignore, 0},
  70.   {"tag", s_ignore, 0},
  71.   {"type", s_ignore, 0},
  72.   {"val", s_ignore, 0},
  73.  
  74. /* other stuff we don't handle */
  75.   {"ABORT", s_ignore, 0},
  76.   {"ident", s_ignore, 0},
  77.  
  78.   {NULL}            /* end sentinel */
  79. };                /* obj_pseudo_table */
  80.  
  81. /* Relocation. */
  82.  
  83. /*
  84.  *        emit_relocations()
  85.  *
  86.  * Crawl along a fixS chain. Emit the segment's relocations.
  87.  */
  88. void
  89. obj_emit_relocations (where, fixP, segment_address_in_file)
  90.      char **where;
  91.      fixS *fixP;        /* Fixup chain for this segment. */
  92.      relax_addressT segment_address_in_file;
  93. {
  94.   for (; fixP; fixP = fixP->fx_next)
  95.     {
  96.       if (fixP->fx_done == 0
  97.       || fixP->fx_r_type != NO_RELOC)
  98.     {
  99.       tc_bout_fix_to_chars (*where, fixP, segment_address_in_file);
  100.       *where += sizeof (struct relocation_info);
  101.     }            /* if there's a symbol */
  102.     }                /* for each fixup */
  103.  
  104. }                /* emit_relocations() */
  105.  
  106. /* Aout file generation & utilities */
  107.  
  108. /* Convert a lvalue to machine dependent data */
  109. void
  110. obj_header_append (where, headers)
  111.      char **where;
  112.      object_headers *headers;
  113. {
  114.   /* Always leave in host byte order */
  115.  
  116.   headers->header.a_talign = section_alignment[SEG_TEXT];
  117.  
  118.   if (headers->header.a_talign < 2)
  119.     {
  120.       headers->header.a_talign = 2;
  121.     }                /* force to at least 2 */
  122.  
  123.   headers->header.a_dalign = section_alignment[SEG_DATA];
  124.   headers->header.a_balign = section_alignment[SEG_BSS];
  125.  
  126.   headers->header.a_tload = 0;
  127.   headers->header.a_dload = md_section_align (SEG_DATA, H_GET_TEXT_SIZE (headers));
  128.  
  129.   headers->header.a_relaxable = linkrelax;
  130.  
  131. #ifdef CROSS_COMPILE
  132.   md_number_to_chars (*where, headers->header.a_magic, sizeof (headers->header.a_magic));
  133.   *where += sizeof (headers->header.a_magic);
  134.   md_number_to_chars (*where, headers->header.a_text, sizeof (headers->header.a_text));
  135.   *where += sizeof (headers->header.a_text);
  136.   md_number_to_chars (*where, headers->header.a_data, sizeof (headers->header.a_data));
  137.   *where += sizeof (headers->header.a_data);
  138.   md_number_to_chars (*where, headers->header.a_bss, sizeof (headers->header.a_bss));
  139.   *where += sizeof (headers->header.a_bss);
  140.   md_number_to_chars (*where, headers->header.a_syms, sizeof (headers->header.a_syms));
  141.   *where += sizeof (headers->header.a_syms);
  142.   md_number_to_chars (*where, headers->header.a_entry, sizeof (headers->header.a_entry));
  143.   *where += sizeof (headers->header.a_entry);
  144.   md_number_to_chars (*where, headers->header.a_trsize, sizeof (headers->header.a_trsize));
  145.   *where += sizeof (headers->header.a_trsize);
  146.   md_number_to_chars (*where, headers->header.a_drsize, sizeof (headers->header.a_drsize));
  147.   *where += sizeof (headers->header.a_drsize);
  148.   md_number_to_chars (*where, headers->header.a_tload, sizeof (headers->header.a_tload));
  149.   *where += sizeof (headers->header.a_tload);
  150.   md_number_to_chars (*where, headers->header.a_dload, sizeof (headers->header.a_dload));
  151.   *where += sizeof (headers->header.a_dload);
  152.   md_number_to_chars (*where, headers->header.a_talign, sizeof (headers->header.a_talign));
  153.   *where += sizeof (headers->header.a_talign);
  154.   md_number_to_chars (*where, headers->header.a_dalign, sizeof (headers->header.a_dalign));
  155.   *where += sizeof (headers->header.a_dalign);
  156.   md_number_to_chars (*where, headers->header.a_balign, sizeof (headers->header.a_balign));
  157.   *where += sizeof (headers->header.a_balign);
  158.   md_number_to_chars (*where, headers->header.a_relaxable, sizeof (headers->header.a_relaxable));
  159.   *where += sizeof (headers->header.a_relaxable);
  160. #else /* ! CROSS_COMPILE */
  161.   append (where, (char *) &headers->header, sizeof (headers->header));
  162. #endif /* ! CROSS_COMPILE */
  163. }                /* a_header_append() */
  164.  
  165. void
  166. obj_symbol_to_chars (where, symbolP)
  167.      char **where;
  168.      symbolS *symbolP;
  169. {
  170.   md_number_to_chars ((char *) &(S_GET_OFFSET (symbolP)), S_GET_OFFSET (symbolP), sizeof (S_GET_OFFSET (symbolP)));
  171.   md_number_to_chars ((char *) &(S_GET_DESC (symbolP)), S_GET_DESC (symbolP), sizeof (S_GET_DESC (symbolP)));
  172.   md_number_to_chars ((char *) &symbolP->sy_symbol.n_value, S_GET_VALUE (symbolP), sizeof (symbolP->sy_symbol.n_value));
  173.  
  174.   append (where, (char *) &symbolP->sy_symbol, sizeof (obj_symbol_type));
  175. }                /* obj_symbol_to_chars() */
  176.  
  177. void
  178. obj_emit_symbols (where, symbol_rootP)
  179.      char **where;
  180.      symbolS *symbol_rootP;
  181. {
  182.   symbolS *symbolP;
  183.  
  184.   /*
  185.      * Emit all symbols left in the symbol chain.
  186.      */
  187.   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
  188.     {
  189.       /* Used to save the offset of the name. It is used to point
  190.            to the string in memory but must be a file offset. */
  191.       char *temp;
  192.  
  193.       temp = S_GET_NAME (symbolP);
  194.       S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
  195.  
  196.       /* Any symbol still undefined and is not a dbg symbol is made N_EXT. */
  197.       if (!S_IS_DEBUG (symbolP) && !S_IS_DEFINED (symbolP))
  198.     S_SET_EXTERNAL (symbolP);
  199.  
  200.       obj_symbol_to_chars (where, symbolP);
  201.       S_SET_NAME (symbolP, temp);
  202.     }
  203. }                /* emit_symbols() */
  204.  
  205. void
  206. obj_symbol_new_hook (symbolP)
  207.      symbolS *symbolP;
  208. {
  209.   S_SET_OTHER (symbolP, 0);
  210.   S_SET_DESC (symbolP, 0);
  211. }
  212.  
  213. static void
  214. obj_bout_line (ignore)
  215.      int ignore;
  216. {
  217.   /* Assume delimiter is part of expression. */
  218.   /* BSD4.2 as fails with delightful bug, so we */
  219.   /* are not being incompatible here. */
  220.   new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
  221.   demand_empty_rest_of_line ();
  222. }                /* obj_bout_line() */
  223.  
  224. void
  225. obj_read_begin_hook ()
  226. {
  227. }
  228.  
  229. void
  230. obj_crawl_symbol_chain (headers)
  231.      object_headers *headers;
  232. {
  233.   symbolS **symbolPP;
  234.   symbolS *symbolP;
  235.   int symbol_number = 0;
  236.  
  237.   tc_crawl_symbol_chain (headers);
  238.  
  239.   symbolPP = &symbol_rootP;    /*->last symbol chain link. */
  240.   while ((symbolP = *symbolPP) != NULL)
  241.     {
  242.       if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_DATA))
  243.     {
  244.       S_SET_SEGMENT (symbolP, SEG_TEXT);
  245.     }            /* if pusing data into text */
  246.  
  247.       resolve_symbol_value (symbolP);
  248.  
  249.       /* OK, here is how we decide which symbols go out into the
  250.      brave new symtab.  Symbols that do are:
  251.         
  252.      * symbols with no name (stabd's?)
  253.      * symbols with debug info in their N_TYPE
  254.         
  255.      Symbols that don't are:
  256.      * symbols that are registers
  257.      * symbols with \1 as their 3rd character (numeric labels)
  258.      * "local labels" as defined by S_LOCAL_NAME(name)
  259.      if the -L switch was passed to gas.
  260.         
  261.      All other symbols are output.  We complain if a deleted
  262.      symbol was marked external. */
  263.  
  264.  
  265.       if (1
  266.       && !S_IS_REGISTER (symbolP)
  267.       && (!S_GET_NAME (symbolP)
  268.           || S_IS_DEBUG (symbolP)
  269. #ifdef TC_I960
  270.       /* FIXME-SOON this ifdef seems highly dubious to me.  xoxorich. */
  271.           || !S_IS_DEFINED (symbolP)
  272.           || S_IS_EXTERNAL (symbolP)
  273. #endif /* TC_I960 */
  274.           || (S_GET_NAME (symbolP)[0] != '\001' && (flag_keep_locals || !S_LOCAL_NAME (symbolP)))))
  275.     {
  276.       symbolP->sy_number = symbol_number++;
  277.  
  278.       /* The + 1 after strlen account for the \0 at the
  279.                end of each string */
  280.       if (!S_IS_STABD (symbolP))
  281.         {
  282.           /* Ordinary case. */
  283.           symbolP->sy_name_offset = string_byte_count;
  284.           string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
  285.         }
  286.       else            /* .Stabd case. */
  287.         symbolP->sy_name_offset = 0;
  288.       symbolPP = &(symbol_next (symbolP));
  289.     }
  290.       else
  291.     {
  292.       if (S_IS_EXTERNAL (symbolP) || !S_IS_DEFINED (symbolP))
  293.         {
  294.           as_bad ("Local symbol %s never defined", S_GET_NAME (symbolP));
  295.         }            /* oops. */
  296.  
  297.       /* Unhook it from the chain */
  298.       *symbolPP = symbol_next (symbolP);
  299.     }            /* if this symbol should be in the output */
  300.     }                /* for each symbol */
  301.  
  302.   H_SET_SYMBOL_TABLE_SIZE (headers, symbol_number);
  303. }
  304.  
  305. /*
  306.  * Find strings by crawling along symbol table chain.
  307.  */
  308.  
  309. void
  310. obj_emit_strings (where)
  311.      char **where;
  312. {
  313.   symbolS *symbolP;
  314.  
  315. #ifdef CROSS_COMPILE
  316.   /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
  317.   md_number_to_chars (*where, string_byte_count, sizeof (string_byte_count));
  318.   *where += sizeof (string_byte_count);
  319. #else /* CROSS_COMPILE */
  320.   append (where, (char *) &string_byte_count, (unsigned long) sizeof (string_byte_count));
  321. #endif /* CROSS_COMPILE */
  322.  
  323.   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
  324.     {
  325.       if (S_GET_NAME (symbolP))
  326.     append (where, S_GET_NAME (symbolP), (unsigned long) (strlen (S_GET_NAME (symbolP)) + 1));
  327.     }                /* walk symbol chain */
  328. }
  329.  
  330. /* end of obj-bout.c */
  331.